﻿using System;
using System.Data;
using System.Windows.Forms;
using BLL;
using Model;
using System.Text.RegularExpressions;
using MongoDB.Driver;
//using System.Linq
//using System.Collections.Generic
//using System.ComponentModel
//using System.Drawing
//using System.Text
//using MongoDB.Bson
//using MongoDB.Driver.GridFS


namespace NCRE学生考试端V1._0
{
    public partial class frmQueryStudentRecord : Form
    {
        private static string con = "mongodb://192.168.22.246:27017";
        MongoServer server;
        //MongoDatabase mydb


        public static string studentID { get; set; }
        public static string Json { get; set; }

        public frmQueryStudentRecord()
        {
            InitializeComponent();

            //mongoDb服务实例连接字符串
            //con = "mongodb://192.168.22.246:27017"
            //得到一个于mongoDB服务器连接的实例
            server = MongoServer.Create(con);
            //获得一个与具体数据库连接对象,数据库名为gywdb
            DB = server.GetDatabase("NCRE");

        }

        #region 窗体单一显示-李金颖-2018-12-8 11:43:47
        private static frmQueryStudentRecord FrmQueryStudentRecord = null;
        public static frmQueryStudentRecord GetInstance()
        {
            if (FrmQueryStudentRecord==null|| FrmQueryStudentRecord.IsDisposed)
            {
                FrmQueryStudentRecord = new frmQueryStudentRecord();
            }
            return FrmQueryStudentRecord;
        }
        #endregion


        DataTable dt = new DataTable();
        StudentInfoEntity studentinfo = new StudentInfoEntity();
        StudentInfoEntityBLL studentinfobll = new StudentInfoEntityBLL();
        private static MongoDatabase DB { get; set; }
        //public static string fileTable = "record"

        private void frmQueryStudentRecord_Load(object sender, EventArgs e)
        {
            AddCheckBoxToDataGridView.dgv = dgv;
            AddCheckBoxToDataGridView.AddFullSelect();
            //DataTable dt 
            DataTable mongodt ;
            //StudentInfoEntityBLL studentinfobll = new StudentInfoEntityBLL()

            //Json串转换为datatable,根据学号查询学号和学生姓名
            //声明一个MongoHelper实例
            MongoHelper mh = new MongoHelper("NCRE");
            string json = mh.GetInfoList("", "01");
            //mongodt = JsonTo.ToDataTable(json)
            mongodt = JsonTo.ToDataTable(json);
            string str;
            string s = string.Empty;
            for (int i = 0; i < mongodt.Rows.Count; i++)
            {
                str = mongodt.Rows[i][0].ToString();
                Regex re = new Regex("(\\d{11})", RegexOptions.None);
                MatchCollection mc = re.Matches(str);
                foreach (Match ma in mc)
                {
                    studentinfo.studentID = mc[0].ToString();

                }

                s += "'" + studentinfo.studentID + "',";
            }
            string studentid = s.Substring(0, s.Length - 1);

            dt = studentinfobll.SelectStudentNameByStudentID(studentid);
            dt.Columns["studentID"].ColumnName = "学号";
            dt.Columns["studentName"].ColumnName = "姓名";
            dgv.DataSource = dt;
            dgv.AllowUserToAddRows = false;

        }

        private void cboCollegeID_SelectedIndexChanged(object sender, EventArgs e)
        {
            //StudentInfoEntity studentinfo = new StudentInfoEntity()
            //StudentInfoEntityBLL studentinfobll = new StudentInfoEntityBLL()
            switch (cboCollegeID.Text)
            {
                case "文学院":
                    studentinfo.CollegeID = "01";
                    break;
                case "社会发展学院":
                    studentinfo.CollegeID = "02";
                    break;
                case "外国语学院":
                    studentinfo.CollegeID = "03";
                    break;
                case "管理学院":
                    studentinfo.CollegeID = "04";
                    break;
                case "美术学院":
                    studentinfo.CollegeID = "05";
                    break;
                case "音乐学院":
                    studentinfo.CollegeID = "06";
                    break;
                case "教育学院":
                    studentinfo.CollegeID = "07";
                    break;
                case "数学与信息科学学院":
                    studentinfo.CollegeID = "08";
                    break;
                case "物理与电子信息学院":
                    studentinfo.CollegeID = "09";
                    break;
                case "化学与材料科学学院":
                    studentinfo.CollegeID = "10";
                    break;
                case "生命科学学院":
                    studentinfo.CollegeID = "11";
                    break;
                case "经济学院":
                    studentinfo.CollegeID = "12";
                    break;
                case "建筑工程学院":
                    studentinfo.CollegeID = "13";
                    break;
                case "体育学院":
                    studentinfo.CollegeID = "14";
                    break;
                case "社科部":
                    studentinfo.CollegeID = "15";
                    break;
                case "其他":
                    studentinfo.CollegeID = "16";
                    break;
                default:
                    studentinfo.CollegeID = "01";
                    break;
            }
            #region"Json串转换为datatable,根据学号查询学号和学生姓名"
            //声明一个MongoHelper实例
            //DataTable dt = new DataTable()
            MongoHelper mh = new MongoHelper("NCRE");
            string json = mh.GetInfoList("", studentinfo.CollegeID);
           DataTable mongodt = JsonTo.ToDataTable(json);
            string str;
            string s = string.Empty;
            for (int i = 0; i < mongodt.Rows.Count; i++)
            {
                str = mongodt.Rows[i][0].ToString();
                Regex re = new Regex("(\\d{11})", RegexOptions.None);
                MatchCollection mc = re.Matches(str);
                foreach (Match ma in mc)
                {
                    studentinfo.studentID = mc[0].ToString();

                }

                s += "'" + studentinfo.studentID + "',";
            }
            if (s.Length == 0)
            {

                dgv.DataSource = "";
                dgv.AllowUserToAddRows = false;
                MessageBox.Show("无该学院学生答题文件");

            }
            else
            {
                string studentid = s.Substring(0, s.Length - 1);
                dt = studentinfobll.SelectStudentNameByStudentID(studentid);
                dt.Columns["studentID"].ColumnName = "学号";
                dt.Columns["studentName"].ColumnName = "姓名";
                dgv.DataSource = dt;
                dgv.AllowUserToAddRows = false;
            }
            #endregion
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            //StudentInfoEntity studentinfo = new StudentInfoEntity()
            //StudentInfoEntityBLL studentinfobll = new StudentInfoEntityBLL()
            switch (cboCollegeID.Text)
            {
                case "文学院":
                    studentinfo.CollegeID = "01";
                    break;
                case "社会发展学院":
                    studentinfo.CollegeID = "02";
                    break;
                case "外国语学院":
                    studentinfo.CollegeID = "03";
                    break;
                case "管理学院":
                    studentinfo.CollegeID = "04";
                    break;
                case "美术学院":
                    studentinfo.CollegeID = "05";
                    break;
                case "音乐学院":
                    studentinfo.CollegeID = "06";
                    break;
                case "教育学院":
                    studentinfo.CollegeID = "07";
                    break;
                case "数学与信息科学学院":
                    studentinfo.CollegeID = "08";
                    break;
                case "物理与电子信息学院":
                    studentinfo.CollegeID = "09";
                    break;
                case "化学与材料科学学院":
                    studentinfo.CollegeID = "10";
                    break;
                case "生命科学学院":
                    studentinfo.CollegeID = "11";
                    break;
                case "经济学院":
                    studentinfo.CollegeID = "12";
                    break;
                case "建筑工程学院":
                    studentinfo.CollegeID = "13";
                    break;
                case "体育学院":
                    studentinfo.CollegeID = "14";
                    break;
                case "社科部":
                    studentinfo.CollegeID = "15";
                    break;
                case "其他":
                    studentinfo.CollegeID = "16";
                    break;
                default:
                    studentinfo.CollegeID = "01";
                    break;
            }
            #region"Json串转换为datatable,根据学号查询学号和学生姓名"
            //声明一个MongoHelper实例
            DataTable mongodt;
            //DataTable dt
            MongoHelper mh = new MongoHelper("NCRE");
            string json = mh.GetInfoList(txtKeyWord.Text.ToString(), studentinfo.CollegeID);
            mongodt = JsonTo.ToDataTable(json);
            string str;
            //string s = string.Empty
            if (mongodt.Rows.Count > 0)
            {
                str = mongodt.Rows[0][0].ToString();
                Regex re = new Regex("(\\d{11})", RegexOptions.None);
                MatchCollection mc = re.Matches(str);
                studentinfo.studentID = mc[0].ToString();
                dt = studentinfobll.SelectVagueStudent(studentinfo);
                dt.Columns["studentID"].ColumnName = "学号";
                dt.Columns["studentName"].ColumnName = "姓名";
            }
            else
            {
                dgv.DataSource = "";
                MessageBox.Show("无该学生答题文件,请确认是否为此学院学生");
            }

        }
            #endregion

        #region "点击checkbox列，选择此行数据"
        private void dgv_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0 && e.RowIndex != -1)
            {
                //MessageBox.Show(this.dgv.Rows[e.RowIndex].Cells[0].EditedFormattedValue.ToString())
                int count = Convert.ToInt32(dgv.Rows.Count.ToString());
                for (int i = 0; i < count; i++)
                {
                    //如果DataGridView是可编辑的，将数据提交，否则处于编辑状态的行无法取到  
                    dgv.EndEdit();
                    DataGridViewCheckBoxCell checkCell = (DataGridViewCheckBoxCell)dgv.Rows[i].Cells["cb_check"];
                    Boolean flag = Convert.ToBoolean(checkCell.Value);
                    if (flag == true)     //查找被选择的数据行  
                    {
                        //从 DATAGRIDVIEW 中获取数据项  
                        studentID = dgv.Rows[i].Cells[1].Value.ToString().Trim();
                        //MessageBox.Show(studentID)
                        ////........................................                }  
                    }
                }
            }
        }
        #endregion

        #region"上传文件"
        private void btnUpload_Click(object sender, EventArgs e)
        {

            //ZipHelper.CreateZip(@"D:\"+studentID, @"D:\"+studentID+@".zip")
            ////申明一个实例
            //MongoHelper mh = new MongoHelper("NCRE")
            ////上传
            //studentinfo.studentID = studentID
            //dt = studentinfobll.SelectCollegeIDByStudentID(studentinfo)
            //string tablename = dt.Rows[0]["CollegeID"].ToString()
            //mh.Upload(@"d:\" + studentID + @".zip", studentID + ".zip", tablename)

        }
        #endregion

        #region"下载文件"
        private void btnDownLoad_Click(object sender, EventArgs e)
        {
            studentinfo.studentID = studentID;
            dt = studentinfobll.SelectCollegeIDByStudentID(studentinfo);
            MongoHelper mh = new MongoHelper("NCRE");
            studentinfo.CollegeID = dt.Rows[0][0].ToString();
            mh.DownLoad(studentID + ".zip", studentinfo.CollegeID);
        }
        #endregion
    }
}